#include "RWLock.h"
#include <pthread.h>
//#include <pthread.cpp>
#include <stdio.h>
#include <stdlib.h>
RWLock RWLOCK = RWLock();

void *pedir_read(void *null){
	printf("Llamo al rlock\n");
	RWLOCK.rlock();
	printf("puedo leer\n");
	sleep(15);
	printf("termine de leer\n");
	RWLOCK.runlock();
//	printf("lo libere\n");
	pthread_exit(NULL);
}


void * pedir_write(void *null){
	printf("Llamo al wlock\n");
	RWLOCK.wlock();
	printf("puedo escribir\n");
	sleep(20);
	printf("termine de escribir\n");
	RWLOCK.wunlock();
//	printf("lo libere\n");
	pthread_exit(NULL);
}


int main(int argc, char *argv[]){
	
	
	pthread_t tid;
	pthread_t tid2;
	pthread_t tid3;
	pthread_t tid4;
	pthread_t tid5;
	pthread_t tid6;

	int rc;
	int rc2;
	int rc3;
	int rc4;
	int rc5;
	int rc6;
	rc = pthread_create(&tid, NULL,pedir_read, NULL);
	rc2 = pthread_create(&tid2,NULL,pedir_write, NULL);
	rc3 = pthread_create(&tid3, NULL,pedir_read, NULL);
	rc4 = pthread_create(&tid4,NULL,pedir_write, NULL);
	rc5= pthread_create(&tid5, NULL,pedir_read, NULL);
	rc6 = pthread_create(&tid6,NULL,pedir_write, NULL);
	if(!rc){	
		pthread_join(tid, NULL);
		printf("termino rc\n");
	}

	if(!rc2){
		pthread_join(tid2, NULL);
		printf("termino rc2\n");
	}
	if(!rc3){	
		pthread_join(tid3, NULL);
		printf("termino rc3\n");
	}

	if(!rc4){
		pthread_join(tid4, NULL);
		printf("termino rc4\n");
	}
	if(!rc5){	
		pthread_join(tid5, NULL);
		printf("termino rc5\n");
	}

	if(!rc6){
		pthread_join(tid6, NULL);
		printf("termino rc6\n");
	}
	
	return 0;
}
